package com.zhuhjay.controller;

import com.zhuhjay.model.domain.HttpResult;
import com.zhuhjay.model.dto.BookInsertDto;
import com.zhuhjay.model.dto.BookUpdateDto;
import com.zhuhjay.model.vo.BookVo;
import com.zhuhjay.model.vo.PageInfoVo;
import com.zhuhjay.service.BookService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author ZhuHJay
 * @date 2023/4/26 16:59
 */
@RestController
@RequestMapping("/book")
public class BookController {

    @Resource
    private BookService bookService;

    @GetMapping("delete.auth")
    public HttpResult<?> delete(@RequestParam("id") Integer id) {
        bookService.delete(id);
        return HttpResult.ok();
    }

    @PostMapping("update.auth")
    public HttpResult<?> update(@RequestBody @Validated BookUpdateDto bookVo) {
        bookService.update(bookVo);
        return HttpResult.ok();
    }

    @GetMapping("one.auth")
    public HttpResult<BookVo> one(@RequestParam("id") Integer id) {
        return HttpResult.ok(bookService.one(id));
    }

    @PostMapping("insert.auth")
    public HttpResult<?> insert(@RequestBody @Validated BookInsertDto bookVo) {
        bookService.insert(bookVo);
        return HttpResult.ok();
    }

    @GetMapping("page/{num}/{size}.auth")
    public HttpResult<PageInfoVo<BookVo>> page(@PathVariable("num") Integer num, @PathVariable("size") Integer size) {
        return HttpResult.ok(bookService.page(num, size));
    }

}
